<?php
class ListAdminForm extends Form{
	function ListAdminForm(){
		Form::Form('ListAdminForm');
	}
	
	function draw(){
		$this->beginForm();	
		global $display;	
		
		AZLib::getCats();

		$acc_groups	 =CGlobal::$group;
		$user_permits=array();
		$user_cats	 =array();
		$all_users	 =array();
		$allist_users=array();
		$gids 		 = '';
		
		foreach ($acc_groups as $group){
			$acc_groups[$group['id']]['users']	=array();
			$acc_groups[$group['id']]['pids']	=array();
			$acc_groups[$group['id']]['pid_str']='';
			if(!in_array($group['id'],array(1,9)))
			$gids.=($gids?',':'').$group['id'];
		}
		
		if($gids){
//			$sql = "SELECT pids,ref_id FROM user_permit WHERE ref_id IN(".$gids.") AND type=0";//lấy quyền của nhóm
			$sql = "SELECT type, ref_id, pids, cids FROM user_permit";//lấy quyền của nhóm
			$result = DB::query($sql);		
			if($result){
				while($row=mysql_fetch_assoc($result)){
					if($row['type']==0){//group
						$acc_groups[$row['ref_id']]['pid_str'].=($acc_groups[$row['ref_id']]['pid_str']?'|':'').$row['pids'];
						
						$pids=explode('|',$row['pids']);
						$acc_groups[$row['ref_id']]['pids']=array();
						
						foreach ($pids as $pid)
						{
							$acc_groups[$row['ref_id']]['pids'][$pid]=CGlobal::$permit[$pid];
						}
					}
					else{//user
						$user_permits[$row['ref_id']]	=$row['pids'];
						$user_cats[$row['ref_id']]		=$row['cids'];
					}
				}
			}
		}
		
		$sql = 'SELECT id, user_name, full_name, gids FROM user AS a WHERE a.gids!="0"';
		$result = DB::query($sql);
		if($result){
			while ($row=mysql_fetch_assoc($result)){
				$row['full_name'] 	= AZLib::trimSpace($row['full_name']);
				$all_users[$row['id']]=$row;
				
				$row['pid_str'] 	= isset($user_permits[$row['id']])?$user_permits[$row['id']]:'';
				$row['cid_str'] 	= isset($user_cats[$row['id']])?$user_cats[$row['id']]:'';
				
				$groups=explode('|',$row['gids']);
				foreach ($groups as $gid){
					if(isset($acc_groups[$gid])){
						if(!User::is_root() && $gid==9)
							$row['del_enable'] 	= 0;
						else
							$row['del_enable'] 	= 1;
						$acc_groups[$gid]['users'][]=$row;
					}
				}
				
				$pid_str= isset($user_permits[$row['id']])?$user_permits[$row['id']]:'';
				$cid_str= isset($user_cats[$row['id']])?$user_cats[$row['id']]:'';
				$pid_arr=explode('|',$pid_str);
				$cid_arr=explode(',',$cid_str);
				$row['pids']=array();
				$row['cids']=array();
				
				if($pid_arr){
					foreach ($pid_arr as $pid){
						if(isset(CGlobal::$permit[$pid]))
						$row['pids'][$pid]=CGlobal::$permit[$pid];
					}
				}
				if($pid_arr){
					foreach ($cid_arr as $cid) {
						if(isset(CGlobal::$allCategories[$cid]))
						$row['cids'][$cid]=CGlobal::$allCategories[$cid];
					}
				}
				
				if($row['pids'] || $row['cids'])
				$allist_users[$row['id']]=$row;
			}				
		}
		
		$display->add('acc_groups',$acc_groups);		
		$display->add('all_users',json_encode($all_users));
		$display->add('allist_users',$allist_users);
		$all_groups=CGlobal::$group;
		if(!User::is_root()){
			unset($all_groups[9]);
		}
		$display->add('all_groups',json_encode($all_groups));
		$display->add('all_permits',json_encode(CGlobal::$permit));
		$display->output('ListAdmin');
		$this->endForm();
	}
}
?>